package com.example.supplychainbackend.controller;

import com.example.supplychainbackend.controller.dto.DataResponse;
import com.example.supplychainbackend.controller.dto.blockchain.UploadInfo;
import com.example.supplychainbackend.controller.dto.user.UserSigDto;
import com.example.supplychainbackend.controller.dto.warehouse.*;
import com.example.supplychainbackend.service.WarehouseService;
import com.example.supplychainbackend.tool.JwtUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/warehouse")
@AllArgsConstructor
public class WarehouseController {

    private final WarehouseService warehouseService;

    @PostMapping("/inbound/{id}/chain")
    public DataResponse<UploadInfo> uploadInboundBill(@PathVariable String id){
        UploadInfo res = warehouseService.uploadInboundBill(id);
        return DataResponse.success(res);
    }

    @PostMapping("/inbound")
    public DataResponse<Long> createInboundBill(@RequestHeader("Authorization") String authHeader,
                                                @RequestBody InboundBillDto form) {
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        form.setUserName(userSigDto.getUserName());
        form.setUserId(userSigDto.getUserId());
        form.setEnterpriseName(userSigDto.getEnterpriseName());
        form.setEnterpriseId(userSigDto.getEnterpriseId());
        Long infoId = warehouseService.createInboundBill(form);
        return DataResponse.success(infoId);
    }

    @PutMapping("/inbound/{id}")
    public DataResponse<InboundBillDto> updateInboundBill(@RequestHeader("Authorization") String authHeader,
                                                          @PathVariable Long id, @RequestBody InboundBillDto form) {
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        form.setUserName(userSigDto.getUserName());
        form.setUserId(userSigDto.getUserId());
        form.setEnterpriseName(userSigDto.getEnterpriseName());
        form.setEnterpriseId(userSigDto.getEnterpriseId());
        InboundBillDto res = warehouseService.updateInboundBill(id, form);
        return DataResponse.success(res);
    }

    @GetMapping("/inbound/list")
    public DataResponse<List<InboundBillDto>> listInboundBills(@RequestHeader("Authorization") String authHeader) {
        List<InboundBillDto> res = warehouseService.getAllInboundBill(0L);
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        String eid = userSigDto.getEnterpriseId();
        if (eid != null) {
            res = warehouseService.getAllInboundBill(Long.valueOf(eid));
        }
        return DataResponse.success(res);
    }

    @GetMapping("/inbound/{id}")
    public DataResponse<InboundBillDto> getInboundBill(@PathVariable Long id) {
        InboundBillDto res = warehouseService.getInboundBill(id);
        return DataResponse.success(res);
    }

    @DeleteMapping("/inbound/{id}")
    public DataResponse<Long> deleteInboundBill(@PathVariable Long id) {
        warehouseService.deleteInboundBill(id);
        return DataResponse.success(id);
    }

    //后端根据入库单自动生成，不再由前端传数据生成
    @PostMapping("/stock")
    public DataResponse<Long> createStock(@RequestBody StockDto form) {
        Long infoId = warehouseService.createStock(form);
        return DataResponse.success(infoId);
    }

    @PutMapping("/stock/{id}")
    public DataResponse<StockDto> updateStock(@PathVariable Long id, @RequestBody StockDto form) {
        StockDto res = warehouseService.updateStock(id, form);
        return DataResponse.success(res);
    }

    @GetMapping("/stock/list")
    public DataResponse<List<StockDto>> listStocks(@RequestHeader("Authorization") String authHeader) {
        List<StockDto> res = warehouseService.getAllStock(0L);
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        String eid = userSigDto.getEnterpriseId();
        if (eid != null) {
            res = warehouseService.getAllStock(Long.valueOf(eid));
        }
        return DataResponse.success(res);
    }

    @GetMapping("/stock/{id}")
    public DataResponse<StockDto> getStock(@PathVariable Long id) {
        StockDto res = warehouseService.getStock(id);
        return DataResponse.success(res);
    }

    @DeleteMapping("/stock/{id}")
    public DataResponse<Long> deleteStock(@PathVariable Long id) {
        warehouseService.deleteStock(id);
        return DataResponse.success(id);
    }

    @PostMapping("/outbound/{id}/chain")
    public DataResponse<UploadInfo> uploadOutboundBill(@PathVariable String id){
        UploadInfo res = warehouseService.uploadOutboundBill(id);
        return DataResponse.success(res);
    }
    @PostMapping("/outbound")
    public DataResponse<Long> createOutboundBill(@RequestHeader("Authorization") String authHeader,
                                                 @RequestBody OutboundBillDto form) {
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        form.setUserName(userSigDto.getUserName());
        form.setUserId(userSigDto.getUserId());
        form.setEnterpriseName(userSigDto.getEnterpriseName());
        form.setEnterpriseId(userSigDto.getEnterpriseId());
        Long infoId = warehouseService.createOutboundBill(form);
        return DataResponse.success(infoId);
    }

    @PutMapping("/outbound/{id}")
    public DataResponse<OutboundBillDto> updateOutboundBill(@RequestHeader("Authorization") String authHeader,
                                                            @PathVariable Long id, @RequestBody OutboundBillDto form) {
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        form.setUserName(userSigDto.getUserName());
        form.setUserId(userSigDto.getUserId());
        form.setEnterpriseName(userSigDto.getEnterpriseName());
        form.setEnterpriseId(userSigDto.getEnterpriseId());
        OutboundBillDto res = warehouseService.updateOutboundBill(id, form);
        return DataResponse.success(res);
    }

    @GetMapping("/outbound/list")
    public DataResponse<List<OutboundBillDto>> listOutboundBills(@RequestHeader("Authorization") String authHeader) {
        List<OutboundBillDto> res = warehouseService.getAllOutboundBill(0L);
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        String eid = userSigDto.getEnterpriseId();
        if (eid != null) {
            res = warehouseService.getAllOutboundBill(Long.valueOf(eid));
        }
        return DataResponse.success(res);
    }

    @GetMapping("/outbound/{id}")
    public DataResponse<OutboundBillDto> getOutboundBill(@PathVariable Long id) {
        OutboundBillDto res = warehouseService.getOutboundBill(id);
        return DataResponse.success(res);
    }

    @DeleteMapping("/outbound/{id}")
    public DataResponse<Long> deleteOutboundBill(@PathVariable Long id) {
        warehouseService.deleteOutboundBill(id);
        return DataResponse.success(id);
    }

    @ExceptionHandler(Exception.class)
    public DataResponse<Exception> warehouseErrorHandler(Exception e) {
        return DataResponse.error(500,e);
    }


    //库区增删改查
    @PostMapping("/setting/area")
    public DataResponse<String> createWareHouseArea(@RequestBody WarehouseAreaDto form) {
        String Id = warehouseService.createWareHouseArea(form);
        return DataResponse.success(Id);
    }

    @PutMapping("/setting/area/{id}")
    public DataResponse<WarehouseAreaDto> updateWareHouseArea(@PathVariable String id, @RequestBody WarehouseAreaDto form) {
        WarehouseAreaDto res = warehouseService.updateWareHouseArea(id, form);
        return DataResponse.success(res);
    }

    @GetMapping("/area/list")
    public DataResponse<List<WarehouseAreaDto>> listAreas() {
        List<WarehouseAreaDto> res = warehouseService.getAllAreas();
        return DataResponse.success(res);
    }

    @GetMapping("/setting/area/{id}")
    public DataResponse<WarehouseAreaDto> getWareHouseArea(@PathVariable String id) {
        WarehouseAreaDto res = warehouseService.getWareHouseArea(id);
        return DataResponse.success(res);
    }

    @DeleteMapping("/setting/area/{id}")
    public DataResponse<String> deleteWareHouseArea(@PathVariable String id) {
        warehouseService.deleteWareHouseArea(id);
        return DataResponse.success(id);
    }

    @PostMapping("/setting/location")
    public DataResponse<Long> createWarehouseLocation(@RequestBody WarehouseLocationDto dto) {
        Long id = warehouseService.createWarehouseLocation(dto);
        return DataResponse.success(id);
    }

    @PutMapping("/setting/location/{id}")
    public DataResponse<WarehouseLocationDto> updateWarehouseLocation(@PathVariable Long id, @RequestBody WarehouseLocationDto dto) throws Exception {
        WarehouseLocationDto res = warehouseService.updateWarehouseLocation(id, dto);
        return DataResponse.success(res);
    }

    @GetMapping("/location/list")
    public DataResponse<List<WarehouseLocationDto>> getListWarehouseLocation() {
        List<WarehouseLocationDto> locationDtos = warehouseService.getListWarehouseLocation();
        return DataResponse.success(locationDtos);
    }

    @GetMapping("/setting/location/{id}")
    public DataResponse<WarehouseLocationDto> getByIdWarehouseLocation(@PathVariable Long id) throws Exception {
        WarehouseLocationDto res = warehouseService.getByIdWarehouseLocation(id);
        return DataResponse.success(res);
    }

    @DeleteMapping("/setting/location/{id}")
    public DataResponse<Long> deleteWarehouseLocation(@PathVariable Long id) {
        long res = warehouseService.deleteWarehouseLocation(id);
        return DataResponse.success(res);
    }

    @PostMapping("/setting/position")
    public DataResponse<Long> createWarehousePosition(@RequestBody WarehousePositionDto dto) {
        Long id = warehouseService.createWarehousePosition(dto);
        return DataResponse.success(id);
    }

    @PutMapping("/setting/position/{id}")
    public DataResponse<WarehousePositionDto> updateWarehousePosition(@PathVariable Long id, @RequestBody WarehousePositionDto dto) throws Exception {
        WarehousePositionDto res = warehouseService.updateWarehousePosition(id, dto);
        return DataResponse.success(res);
    }

    @GetMapping("/position/list")
    public DataResponse<List<WarehousePositionDto>> getListWarehousePosition() {
        List<WarehousePositionDto> positionDtos = warehouseService.getListWarehousePosition();
        return DataResponse.success(positionDtos);
    }

    @GetMapping("/setting/position/{id}")
    public DataResponse<WarehousePositionDto> getByIdWarehousePosition(@PathVariable Long id) throws Exception {
        WarehousePositionDto res = warehouseService.getByIdWarehousePosition(id);
        return DataResponse.success(res);
    }

    @DeleteMapping("/setting/position/{id}")
    public DataResponse<Long> deleteWarehousePosition(@PathVariable Long id) {
        long res = warehouseService.deleteWarehousePosition(id);
        return DataResponse.success(res);
    }

    @PostMapping("/setting/position/bind/{id}")
    public DataResponse<Long> bindWarehousePositionDevice(@PathVariable Long id, @RequestBody BindDeviceReqDto dto) {
        Long res = warehouseService.bindWarehousePositionDevice(id, dto);
        return DataResponse.success(res);
    }

}
